package org.dding.mvc.web.tag;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import org.dding.core.util.StringUtils;

public class CollectionFunction {

	public static List<String> convert2List(String list, String split) {

		List<String> l = new ArrayList<String>();

		if (StringUtils.isEmpty(list))
			return l;

		if (StringUtils.isEmpty(split)) {
			l.add(list);
			return l;
		}

		StringTokenizer token = new StringTokenizer(list, split);
		while (token.hasMoreTokens()) {
			l.add(token.nextToken());
		}
		return l;
	}

	public static boolean isMap(Object obj) {
		if (obj instanceof Map) {
			return true;
		}
		return false;
	}

	@SuppressWarnings("rawtypes")
	public static Object getMapValue(Map map, String key) {

		if (map != null) {
			return map.get(key);
		}
		return null;
	}

	public static boolean isList(Object obj) {
		if (obj instanceof List) {
			return true;
		}
		return false;
	}

	public static boolean contains(Object object, Object obj) {

		if (object == null)
			return false;
		if (object instanceof Collection) {

			Collection<?> col = Collection.class.cast(object);
			if (col.contains(obj))
				return true;
			else
				return false;
		} else {

			Object[] objects = (Object[]) object;
			for (Object o : objects) {
				if (o.equals(obj))
					return true;
			}
			return false;
		}
	}

	public static boolean containsKey(Map<String, Object> map, String key) {

		if (map == null)
			return false;
		if (map.containsKey(key))
			return true;
		else
			return false;
	}

	public static boolean containsValue(Map<String, Object> map, Object value) {

		if (map == null)
			return false;
		if (map.containsValue(value))
			return true;
		else
			return false;
	}
}
